package org.xhnews.parsers;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.Vector;

import net.rim.device.api.i18n.DateFormat;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.io.http.HttpDateParser;

import org.xhnews.log.RaspberryLogger;
import org.xhnews.to.RssItem;
import org.xhnews.utils.StringUtil;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;



public class RssPullXMLParser {
	public static final String tag = "RssPullXMLParser";
	public static DateFormat DATEFORMAT=new SimpleDateFormat("yyyy-MM-dd HH:mm");
	//解析资讯列表
	public static Vector readRssItems(InputStream inputStream,
            String inputEncoding,String channelId) throws Exception {
	   // 创建Pull解析
	   XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
	   XmlPullParser pullParser = pullParserFactory.newPullParser();
	   // 解析XML	  
	   Reader reader=new InputStreamReader(inputStream,"gb2312");
	   reader.read();
	   pullParser.setInput(reader);	  
	   // 开始
	   int eventType = pullParser.getEventType(); 
	   Vector items = null;
	   RssItem item = null;
	   try{
		  while (eventType != XmlPullParser.END_DOCUMENT) {
	            String nodeName = pullParser.getName();	 
	            switch (eventType) {
	            // 文档开始
	            case XmlPullParser.START_DOCUMENT:
	            	items = new Vector();
	                break;
	            // 节点开始
	            case XmlPullParser.START_TAG:
	                     if ("item".equals(nodeName)) {
	                    	 item=new RssItem();	 	                    	 
                          }else if("title".equals(nodeName)){
                        	  if(item!=null){
                        	    item.setTitle(pullParser.nextText());
                        	    //AlexLog.log(tag, item.getTitle());
                        	  }
                          }else if("link".equals(nodeName)){
                        	  if(item!=null){
                        	    item.setLink(pullParser.nextText()); 
                        	  }
                          }else if("description".equals(nodeName)){
                        	  if(item!=null){                        	    
                        	    String temp=pullParser.nextText();  
//                                int beginIndex=temp.indexOf("<img");
//                                int endIndex=temp.indexOf(">", beginIndex);
//                                String subtemp=temp.substring(beginIndex, endIndex);
//                                temp=StringUtil.replaceAll(subtemp, "", temp);  
                        	    item.setDescription(temp); 
                        	  }
                          }else if("pubDate".equals(nodeName)){
                        	  if(item!=null){
                        		String temp=pullParser.nextText();
                        		Date dt=new Date(HttpDateParser.parse(temp));
                        		temp=DATEFORMAT.format(dt);
                        	    item.setPubDate(temp);
                        	  }
                          }else if("source".equals(nodeName)){
                        	  if(item!=null){
                        	    item.setSource(pullParser.nextText());
                        	  }
                          }else if("author".equals(nodeName)){
                        	  if(item!=null){
                        	    item.setAuthor(pullParser.nextText());
                        	  }
                          }	                     
	                     break;
	            // 节点结束
	            case XmlPullParser.END_TAG:
	                     if ("item".equals(nodeName)) {
	                    	 item.setRead(false);
	                    	 item.setChannelId(channelId);
	                    	 items.addElement(item);
	                         item= null;
	                     }
	                     break;
	            }
	            eventType = pullParser.next();
	   }//end while
	   return items;
	   }catch(Exception e){
		   RaspberryLogger.getInstance().info(e.getMessage());
		   e.printStackTrace();		  
		   return null;
	   }
	}
}
